package MultiThread._feizhu;

import java.util.concurrent.Semaphore;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/6/24 15:11
 * 说明：
 */

public class PrintALI {
    public static void main(String[] args) {
        // 信号量
        final Semaphore s1 = new Semaphore(1);
        final Semaphore s2 = new Semaphore(0);
        final Semaphore s3 = new Semaphore(0);
        // 创建三个线程分别打印 a、l、i
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    try {
                        s1.acquire();
                        System.out.print("a");
                        s2.release();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    try {
                        s2.acquire();
                        System.out.print("l");
                        s3.release();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    try {
                        s3.acquire();
                        System.out.print("i");
                        s1.release();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

        // 开启线程
        t1.start();
        t2.start();
        t3.start();
    }
}
